package com.tianji.chat.mq;

import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.chat.domain.po.Chat;
import com.tianji.chat.domain.po.Task;
import com.tianji.chat.service.IChatService;

import com.tianji.chat.service.ITaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class LikesChangeListener {

    private final IChatService chatService;
    private final ITaskService taskService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "chat.liked.times.queue", durable = "true"),
            exchange = @Exchange(name = "like.record.topic", type = ExchangeTypes.TOPIC),
            key = "CHAT.times.changed"
    ))
    public void listenChatLikedTimesChange(List<LikedTimesDTO> likedTimesDTOs){
        log.debug("监听到大模型的回答的点赞数变更");

        List<Chat> list = new ArrayList<>(likedTimesDTOs.size());
        for (LikedTimesDTO dto : likedTimesDTOs) {
            Chat r = new Chat();
            r.setId(dto.getBizId());
            r.setLikedTimes(dto.getLikedTimes());
            list.add(r);
        }
        chatService.updateBatchById(list);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "task.liked.times.queue", durable = "true"),
            exchange = @Exchange(name = "like.record.topic", type = ExchangeTypes.TOPIC),
            key = "TASK.times.changed"
    ))
    public void listenTaskLikedTimesChange(List<LikedTimesDTO> likedTimesDTOs){
        log.debug("监听到大模型的任务的点赞数变更");

        List<Task> list = new ArrayList<>(likedTimesDTOs.size());
        for (LikedTimesDTO dto : likedTimesDTOs) {
            Task r = new Task();
            r.setId(dto.getBizId());
            r.setLikedTimes(dto.getLikedTimes());
            list.add(r);
        }
        taskService.updateBatchById(list);
    }
}
